import { baseModule } from "@/base/module";
import { elevatorCatGame } from "@/elevatorCat/game";
import { LazyLoader } from "@sapi-game/gameComponent/common/lazyLoader";
import { RegionTeamChooser } from "@sapi-game/gameComponent/region/regionTeamChooser";
import { RegionTeamCleaner } from "@sapi-game/gameComponent/region/regionTeamCleaner";
import { GamePlayer } from "@sapi-game/gamePlayer/gamePlayer";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { CubeRegion } from "@sapi-game/gameRegion/gameRegion";
import { Game } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { ElevatorBackActionBar } from "./actionbar";
import { EleBackStateButtons } from "./buttons";
import { ElevatorCatConfigSetter } from "./configSetter";

export class eleBackState extends baseModule.State {
    cat: PlayerGroup = new PlayerGroup(GamePlayer);
    mouse: PlayerGroup = new PlayerGroup(GamePlayer);
    waitingRegion = new CubeRegion(
        DimensionIds.Overworld,
        { x: 359, y: -50, z: 163 },
        { x: 377, y: -61, z: 146 }
    );

    override onEnter(): void {
        this.addComponent(LazyLoader, {
            dimensionId: DimensionIds.Overworld,
            pos: { x: 369, y: -60, z: 152 },
            onLoad: this.onLoad.bind(this),
            onUnload: this.onUnload.bind(this),
        }).addComponent(ElevatorBackActionBar);
    }

    getTotalPlayers() {
        return this.cat.validSize + this.mouse.size;
    }

    onLoad(loader: LazyLoader) {
        loader
            .addComponent(RegionTeamChooser, {
                config: [
                    {
                        region: new CubeRegion(
                            DimensionIds.Overworld,
                            { x: 363, y: -59, z: 159 },
                            { x: 365, y: -59, z: 157 }
                        ),
                        team: this.mouse,
                        onJoin: (player) => {
                            player.sendMessage("§a成功加入躲躲队伍");
                            player.player?.playSound("note.bell");
                        },
                        onEnter(p) {
                            if (this.team.has(p))
                                p.actionbar("§a你已经在躲躲队伍中了");
                        },
                    },
                    {
                        region: new CubeRegion(
                            DimensionIds.Overworld,
                            { x: 372, y: -59, z: 159 },
                            { x: 374, y: -59, z: 157 }
                        ),
                        team: this.cat,
                        onJoin: (player) => {
                            player.sendMessage("§b成功加入猫猫队伍");
                            player.player?.playSound("note.bell");
                        },
                        onEnter(p) {
                            if (this.team.has(p))
                                p.actionbar("§b你已经在猫猫队伍中了");
                        },
                    },
                ],
            })
            .addComponent(RegionTeamCleaner, {
                region: this.waitingRegion,
                teams: [this.cat, this.mouse],
            })
            .addComponent(ElevatorCatConfigSetter)
            .addComponent(EleBackStateButtons);
    }

    onUnload() {
        this.cat.clear();
        this.mouse.clear();
    }

    startGame() {
        if (
            (this.mouse.validSize >= 1 && this.cat.validSize >= 1) ||
            (Game.config.config.debugMode &&
                this.cat.validSize + this.mouse.validSize > 0)
        ) {
            if (Game.manager.hasGame(elevatorCatGame)) {
                this.waitingRegion.runCommandOnPlayers(
                    "title @s title 无法重复开始"
                );
                return;
            }
            Game.manager.startGame(elevatorCatGame, {
                cat: this.cat,
                mouse: this.mouse,
            });
            this.getComponent(LazyLoader).reload();
        } else {
            this.waitingRegion.runCommandOnPlayers(
                "title @s title 玩家数量不足"
            );
        }
    }
}
